import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class TimePickerDemo extends StatefulWidget {
  @override
  _TimePickerDemoState createState() => _TimePickerDemoState();
}

class _TimePickerDemoState extends State<TimePickerDemo> {
  int selectedHour = 8;
  int selectedMinute = 26;

  final List<int> hours = List.generate(24, (i) => i);       // 0~23 小时
  final List<int> minutes = List.generate(60, (i) => i);     // 0~59 分钟

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          height: 200,
          decoration: BoxDecoration(
            color: Colors.grey[300],
            borderRadius: BorderRadius.circular(40),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 小时选择
              Expanded(
                child: CupertinoPicker(
                  scrollController: FixedExtentScrollController(
                    initialItem: hours.indexOf(selectedHour),
                  ),
                  itemExtent: 40,
                  onSelectedItemChanged: (index) {
                    setState(() {
                      selectedHour = hours[index];
                    });
                  },
                  children: hours.map((h) => Center(child: Text('$h h'))).toList(),
                ),
              ),
              // 分钟选择
              Expanded(
                child: CupertinoPicker(
                  scrollController: FixedExtentScrollController(
                    initialItem: minutes.indexOf(selectedMinute),
                  ),
                  itemExtent: 40,
                  onSelectedItemChanged: (index) {
                    setState(() {
                      selectedMinute = minutes[index];
                    });
                  },
                  children: minutes.map((m) => Center(child: Text('$m m'))).toList(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
